package xyz.archknight.app.loginput.dictionary.gen.service

import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import xyz.archknight.app.loginput.dictionary.gen.bean.DatabaseProtocol
import xyz.archknight.app.loginput.dictionary.gen.dao.Caches
import xyz.archknight.app.loginput.dictionary.gen.dao.Word
import xyz.archknight.app.loginput.dictionary.gen.dao.Words

/**
 * 缓存数据向主词库转移工具
 *
 * @author midnite
 * create at 2020-06-22 3:54 下午
 */
fun transferCache() {
    transaction {
        val phaseInWords = Word.all().map { it.phase }
        Words.insert(
            Caches.slice(Caches.phase, Caches.pinyin).select { Caches.phase notInList phaseInWords },
            columns = listOf(Words.phase, Words.pinyin)
        )
        SchemaUtils.drop(Caches)
        SchemaUtils.createMissingTablesAndColumns(Caches)
    }
    EventBus.dbChange(DatabaseProtocol.RefreshBoth)
}